package com.youcai.springboot_day06_security.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
public class loginConfig extends WebSecurityConfigurerAdapter {


    //链式编程
    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
       //首页所有人可以访问
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //没有权限，去登录页面
        http.formLogin().loginPage("/login");

        //防止网站工具
        http.csrf().disable();

        //记住我功能
        http.rememberMe().rememberMeParameter("aaa");


        //注销
        http.logout().logoutSuccessUrl("/");
    }

  //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("aa").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("bb").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1")
                .and()
                .withUser("cc").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip1");

    }
}
